#ifndef _T9MANAGER_H
#define _T9MANAGER_H
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <time.h>
#define true 1
#define false 0
#define wordlen 35


//DATA STRUCTURE FOR WORDS LIST
struct parola {
	char *str;
	int count;
	struct parola* next;
	struct parola* prev;
};


//DATA STRUCTURE TO MANAGE WORD STORAGE IN MEMORY. IT'S LIKE A M-TREE. EVERY NODE CONTAINS THE WORDS LIST ACCORDING TO THEIR NUMERIC T9 SEQUENCE
struct elementonumerico {	
	struct elementonumerico *element2;
	struct elementonumerico *element3;
	struct elementonumerico *element4;
	struct elementonumerico *element5;
	struct elementonumerico *element6;
	struct elementonumerico *element7;
	struct elementonumerico *element8;
	struct elementonumerico *element9;
	struct elementonumerico* elementoprec;
	struct parola *paroleHead;
	struct parola *paroleTail;
};


/*HERE GO ALL THE VARIABLES YOU NEED TO CONTROL*/
typedef struct _Data Data;
struct _Data
{
	GtkWidget  *textarea;
	GtkBuilder *builder;
	GtkWidget *the_window1;
	GtkStatusIcon *trayIcon;	
	char* tempnum;
	char* tempchar;
	char* punctuation;
	char* symbols;	
	struct elementonumerico* listat9;//THE HEAD OF OUR STRUCTURE
	struct elementonumerico* currentnode;
	struct parola* currentword;
	char* tempstring;
	int charnumber;
	int numflag;
	time_t begin;
	int lastClickedButton;
	int state;
};


//ROUTINES TO MANAGE TIMING IN MANUAL MODE INPUT
clock_t BeginTimer();
clock_t ElapsedTimer(clock_t begin);
//----------------------------------------------


//ROUTINES TO MANAGE THE MAIN STRUCTURE AND I/O WITH THE DICTIONARY_FILE
void initData(Data *d);//
void inserisciParola(struct parola **head,struct parola **tail,char *str,int count);
struct elementonumerico* inserisciNuovoNodo(struct elementonumerico **nuovoNodo,struct elementonumerico*);
struct elementonumerico* inserisciElemento(struct elementonumerico **lista, char* cod, char* word,int count);
struct elementonumerico* initDictionary(char* path,Data d);
struct elementonumerico *findNodeFromCode(struct elementonumerico *fromNode, char toNode);
void saveDictionary(struct elementonumerico* listat9,char* path);
void visit(struct elementonumerico* node, FILE* fp);
void preorder(struct elementonumerico* node, FILE* fp, void (*visit)(struct elementonumerico* ele,FILE* fp));
void visitWordList(struct elementonumerico* node, FILE* fp);
//--------------------------------------------------------------------------------------------------------------


//ROUTINES TO SEARCH FOR WORD IN THE MAIN STRUCTURE
char* findWord(char code,Data* d);
char* findNextWord(struct parola *fromNode,struct parola* head,Data* d);
char* findPrevWord(Data* d);
char* getCodeFromString(char* currentString);
//---------------------------------------------------------------------------------------------------------------


//ROUTINES THAT SEND TEXT TO THE FOCUSED WINDOW (USING XLIB FUNCTIONALITIES)
void SendEvent(XKeyEvent *event, int press);
int sendChar(int c,int state);
//--------------------------------------------------------------------------


//GTK STUFF
GtkWidget *get_object_from_builder(GtkBuilder *builder, gchar *obj_id);
void add_all_events(Data *d);
void on_button1_clicked(GtkButton *button, Data *data);
void on_button2_clicked(GtkButton *button, Data *data);
void on_button3_clicked(GtkButton *button, Data *data);
void on_button4_clicked(GtkButton *button, Data *data);
void on_button5_clicked(GtkButton *button, Data *data);
void on_button6_clicked(GtkButton *button, Data *data);
void on_button7_clicked(GtkButton *button, Data *data);
void on_button8_clicked(GtkButton *button, Data *data);
void on_button9_clicked(GtkButton *button, Data *data);
void on_button0_clicked(GtkButton *button, Data *data);
void on_buttoncancell_clicked(GtkButton *button, Data *data);
void on_buttonasterix_clicked(GtkButton *button, Data *data);
void on_buttonClear_clicked(GtkButton *button, Data *data);
void on_togglebuttonNew_toggled(GtkToggleButton *button, Data *data);
void on_togglebuttonCaps_toggled(GtkToggleButton *button, Data *data);
void on_togglebuttonNum_toggled(GtkToggleButton *button, Data *data);
void on_togglebuttonLock_toggled(GtkToggleButton *button, Data *data);
static void trayIconActivated(GObject *trayIcon, gpointer data);
static void destroy (GtkWidget* wdg, Data* d);
static gboolean delete_event (GtkWidget*, GdkEvent*, gpointer);
static gboolean window_state_event (GtkWidget *widget, GdkEventWindowState *event, gpointer user_data);
//-------------------------------------------------------------------------------------------------------


//UTILITIES
char* substring_r(char* buffer, char* str, int start, int end);
int hasNumber(char* str);
int charToKeyCode(char currentChar);
//---------------------------------------------------------------

#endif
